package yx.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import yx.dao.BaseDaoInt;
import yx.model.BsStudent;
import yx.model.BsStudentOther;
import yx.model.SmCode;
import yx.vo.DatagridVO;
import yx.vo.StudentOtherVO;

@Service("studentOtherService")
public class StudentOtherServiceImpl implements StudentOtherServiceInt {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(StudentOtherServiceImpl.class);

	private BaseDaoInt<BsStudentOther> studentOtherDao;
	private BaseDaoInt<BsStudent> studentDao;
	private BaseDaoInt<SmCode> codeDao;

	public BaseDaoInt<BsStudentOther> getStudentOtherDao() {
		return studentOtherDao;
	}

	@Autowired
	public void setStudentOtherDao(BaseDaoInt<BsStudentOther> studentOtherDao) {
		this.studentOtherDao = studentOtherDao;
	}

	public BaseDaoInt<BsStudent> getStudentDao() {
		return studentDao;
	}

	@Autowired
	public void setStudentDao(BaseDaoInt<BsStudent> studentDao) {
		this.studentDao = studentDao;
	}

	public BaseDaoInt<SmCode> getCodeDao() {
		return codeDao;
	}

	@Autowired
	public void setCodeDao(BaseDaoInt<SmCode> codeDao) {
		this.codeDao = codeDao;
	}

	@Override
	public StudentOtherVO save(StudentOtherVO studentOther) {
		BsStudentOther t = new BsStudentOther();
		BeanUtils.copyProperties(studentOther, t);
		studentOtherDao.save(t);
		BeanUtils.copyProperties(t, studentOther);
		return studentOther;
	}

	@Override
	public StudentOtherVO edit(StudentOtherVO studentOther) {
		BsStudentOther t = studentOtherDao.get(BsStudentOther.class, studentOther.getId());
		BeanUtils.copyProperties(studentOther, t);
		return studentOther;
	}

	@Override
	public void remove(String ids) {
		String[] nids = ids.split(",");
		String hql = "delete BsStudentOther t where t.id in (";
		for (int i = 0; i < nids.length; i++) {
			if (i > 0) {
				hql += ",";
			}
			hql += "'" + nids[i] + "'";
		}
		hql += ")";
		studentOtherDao.executeHql(hql);
	}

	@Override
	public DatagridVO datagrid(StudentOtherVO studentOther) {
		DatagridVO dg = new DatagridVO();
		String hql = "from BsStudentOther t";
		Map<String, Object> params = new HashMap<String, Object>();
		hql = addWhere(studentOther, hql, params);
		String totalHql = "select count(*) " + hql;
		hql = addOrder(studentOther, hql);
		List<BsStudentOther> l = studentOtherDao.find(hql, params, studentOther.getPage(), studentOther.getRows());
		List<StudentOtherVO> nl = new ArrayList<StudentOtherVO>();
		changeModel(l, nl);
		dg.setTotal(studentOtherDao.count(totalHql, params));
		dg.setRows(nl);
		return dg;
	}

	private void changeModel(List<BsStudentOther> dbList, List<StudentOtherVO> nl) {
		if (dbList != null && dbList.size() > 0) {
			for (BsStudentOther studentOther : dbList) {
				StudentOtherVO vo = new StudentOtherVO();
				BeanUtils.copyProperties(studentOther, vo);
				nl.add(vo);
			}
		}
	}

	private String addOrder(StudentOtherVO studentOther, String hql) {
		if (studentOther.getSort() != null || studentOther.getOrder() != null) {
			hql += " order by " + studentOther.getSort() + " " + studentOther.getOrder();
		}
		return hql;
	}

	private String addWhere(StudentOtherVO studentOther, String hql, Map<String, Object> params) {
		if (studentOther.getStudentId() != null) {
			hql += " where t.studentId = :studentId";
			params.put("studentId", studentOther.getStudentId());
		}
		return hql;
	}

}
